﻿/*
 * Reflector from Microsoft.Synchronization.Data.DbSyncBatchInfoFactory
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Synchronization.Data;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace SyncLib.Data
{

  public  class KpSyncBatchInfoFactory
    {
      public static DbSyncBatchInfo Deserialize(string batchFileName)
        {
            return Deserialize(batchFileName, true);
        }

        public static DbSyncBatchInfo Deserialize(string batchFileName, bool deserializeData)
        {
            DbSyncBatchInfo info2;
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Binder = new KpSyncBatchSerializationBinder();
            FileStream serializationStream = new FileStream(batchFileName, FileMode.Open);
            try
            {
                DbSyncBatchInfo info = (DbSyncBatchInfo)formatter.Deserialize(serializationStream);
                if (deserializeData)
                {
                    info.DataSetSurrogate = (DataSetSurrogate)formatter.Deserialize(serializationStream);
                }
                info2 = info;
            }
            finally
            {
                serializationStream.Close();
            }
            return info2;
        }

        public static void Serialize(DbSyncBatchInfo batchInfo, string batchFileName)
        {
            BinaryFormatter formatter = new BinaryFormatter();
            FileStream serializationStream = new FileStream(batchFileName, FileMode.Create);
            try
            {
                formatter.Serialize(serializationStream, batchInfo);
                formatter.Serialize(serializationStream, batchInfo.DataSetSurrogate);
                serializationStream.Flush();
            }
            finally
            {
                serializationStream.Close();
            }
        }
    }

}
